package adv9;

import java.net.*;
import java.io.*;

public class FileSendThread extends Thread {
	private Socket s;

	public FileSendThread(Socket s) {
		this.s = s;
	}

	// 发文件的步骤是 读取文件为字节，发送；
	// client:接受字节，保存到文件中
	@Override
	public void run() {
		// Map<fileName,byte[])
		String fileName = "D:/Documents/personal/job-salary.png";
		try {
			FileInputStream fin = new FileInputStream(fileName);
			byte b[] = new byte[fin.available()];
			int len = fin.read(b);
			fin.close();
			OutputStream out = s.getOutputStream();
			out.write(b, 0, len);
			out.flush();
			System.out.println("发送文件给" + s.getInetAddress() + ":" + s.getPort()
					+ "成功！");
		} catch (Exception e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		} finally {
			if (s != null)
				try {
					s.close();
				} catch (IOException e) {
					// TODO 自动生成的 catch 块
					e.printStackTrace();
				}
		}

	}
}
